<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="SPU" prop="spu">
        <el-input
            v-model="queryParams.spu"
            placeholder="请输入SPU"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="spu">
        <el-input
            v-model="queryParams.name"
            placeholder="请输入商品名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="跟进" prop="productId">
        <el-select v-model="queryParams.genjin" clearable filterable  style="width:120px;">
          <el-option
              v-for="item in genjinList"
              :key="item.id"
              :label="item.val"
              :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="店铺" prop="dianpu">
        <el-select v-model="queryParams.dianpu" clearable filterable style="width:120px;">
          <el-option
              v-for="item in shopNameList"
              :key="item.id"
              :label="item.val"
              :value="item.val"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Plus" v-hasPermi="['mxerp:tkshop:add']" @click="handleAdd">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="tkshopList" >
      <el-table-column label="SPU图" align="center" class-name="small-padding fixed-width" width="80" >
        <template #default="scope">
          <el-image style="width: 70px; height: 70px" :src="scope.row.img" />
        </template>
      </el-table-column>
      <el-table-column label="商品" align="center" prop="name" />
      <el-table-column label="SPU" align="center" prop="spu" />
      <el-table-column label="店铺" align="center" prop="dianpu" />
      <el-table-column label="进货价格" align="center" prop="price" />
      <el-table-column label="售价" align="center" prop="outprice" />
      <el-table-column label="规格" align="center" prop="ggname" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="美国" align="center" prop="us" >
        <template #default="scope">
            <el-switch inline-prompt active-text="在售" :active-value=1 active-color="#2fa1f1"
                       inactive-text="下架" :inactive-value=0 inactive-color="#9c9c9c"
                       v-model="scope.row.us" @change="handleUs(scope.row)"/>
        </template>
      </el-table-column>
      <el-table-column label="英国" align="center" prop="uk" >
        <template #default="scope">
          <el-switch inline-prompt active-text="在售" :active-value=1 active-color="#2fa1f1"
                     inactive-text="下架" :inactive-value=0 inactive-color="#9c9c9c"
                     v-model="scope.row.uk" @change="handleUk(scope.row)"/>
        </template>
      </el-table-column>
      <el-table-column label="沙特" align="center" prop="shate" >
        <template #default="scope">
          <el-switch inline-prompt active-text="在售" :active-value=1 active-color="#2fa1f1"
                     inactive-text="下架" :inactive-value=0 inactive-color="#9c9c9c"
                     v-model="scope.row.shate" @change="handleShate(scope.row)"/>
        </template>
      </el-table-column>
      <el-table-column label="跟进" align="center" prop="genjin" >
        <template #default="scope">
          <el-switch inline-prompt active-text="跟进" :active-value=49 active-color="#2fa1f1"
                     inactive-text="否" :inactive-value=50 inactive-color="#9c9c9c"
                     v-model="scope.row.genjin" @change="handleGenJin(scope.row)"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['mxerp:tkshop:edit']"></el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['mxerp:tkshop:remove']"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改tk商店管理对话框 -->
    <el-dialog :title="title" v-model="open" width="80%" append-to-body>
      <el-form ref="tkshopRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="SPU" prop="spu">
          <el-input v-model="form.spu" placeholder="请输入SPU" />
        </el-form-item>
        <el-form-item label="SKU" prop="sku">
          <el-input v-model="form.sku" placeholder="请输入SKU" />
        </el-form-item>
        <el-form-item label="商品" prop="productId">
          <el-select v-model="form.productId" filterable style="width:100%;">
            <el-option
                v-for="item in productlist"
                :key="item.id"
                :label="item.value"
                :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="店铺" prop="dianpu">
          <el-select v-model="form.dianpu" filterable style="width:100%;">
            <el-option
                v-for="item in shopNameList"
                :key="item.id"
                :label="item.val"
                :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="进货价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入进货价格" />
        </el-form-item>
        <el-form-item label="售价" prop="outprice">
          <el-input v-model="form.outprice" placeholder="请输入售价" />
        </el-form-item>
        <el-form-item label="颜色" prop="color">
          <el-input v-model="form.color" placeholder="请输入颜色" />
        </el-form-item>
        <el-form-item label="规格" prop="gg">
          <el-select v-model="form.gg" filterable style="width:100%;">
            <el-option
                v-for="item in ggList"
                :key="item.id"
                :label="item.val"
                :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" filterable style="width:100%;">
            <el-option
                v-for="item in statusList"
                :key="item.id"
                :label="item.val"
                :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="是否跟进" prop="genjin">
          <el-select v-model="form.genjin" filterable style="width:100%;">
            <el-option
                v-for="item in genjinList"
                :key="item.id"
                :label="item.val"
                :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Tkshop">
import { ref } from 'vue'
import { listTkshop, getTkshop, delTkshop, addTkshop, updateTkshop } from "@/api/mxerp/tkshop";
import { getAllProduct } from "@/api/mxerp/product";
import { getParamByPid } from "@/api/bydll/param";



const { proxy } = getCurrentInstance();
const tkshopList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const productlist = ref([]);
const shopNameList = ref([]);
const statusList = ref([]);
const genjinList = ref([]);
const ggList = ref([]);


const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 50,
    spu: null,
    sku: null,
    productId: null,
    dianpu: null,
    price: null,
    outprice: null,
    color: null,
    gg: null,
    us: null,
    uk: null,
    shate: null,
    addtime: null,
    upTime: null,
    status: null,
    genjin: null,
    name: null,
    img: null,
    model: null
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询tk商店管理列表 */
function getList() {
  loading.value = true;
  // console.log(JSON.stringify(queryParams.value));
  // queryParams.value.status = null;
  listTkshop(queryParams.value).then(response => {
    tkshopList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

function handleUs(row){
  // console.log(JSON.stringify(row));
  row.dianpu = null;
  row.status = null;
  updateTkshop(row).then(response => {
  });
}

function handleUk(row){
  // console.log(JSON.stringify(row));
  row.dianpu = null;
  row.status = null;
  updateTkshop(row).then(response => {
  });
}

function handleShate(row){
  // console.log(JSON.stringify(row));
  row.dianpu = null;
  row.status = null;
  updateTkshop(row).then(response => {
  });
}

function handleGenJin(row){
  // console.log(JSON.stringify(row));
  row.dianpu = null;
  row.status = null;
  updateTkshop(row).then(response => {
  });
}



function loadSelData(){
  getAllProduct().then(response => {
    productlist.value = response.data;
  });

  let param = {"pid":"38"};
  getParamByPid(param).then(resp => {
    shopNameList.value = resp.data
  })

  param = {"pid":"41"};
  getParamByPid(param).then(resp => {
    statusList.value = resp.data
  })

  param = {"pid":"48"};
  getParamByPid(param).then(resp => {
    genjinList.value = resp.data
  })

  param = {"pid":"33"};
  getParamByPid(param).then(resp => {
    ggList.value = resp.data
  })

}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    spu: null,
    sku: null,
    productId: null,
    dianpu: null,
    price: null,
    outprice: null,
    color: null,
    gg: null,
    us: null,
    uk: null,
    shate: null,
    remark: null,
    addtime: null,
    upTime: null
  };
  proxy.resetForm("tkshopRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加tk商店管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getTkshop(_id).then(response => {
    form.value = response.data;
    form.value.dianpu = parseInt(response.data.dianpu);
    form.value.gg = parseInt(response.data.gg);
    open.value = true;
    title.value = "修改tk商店管理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["tkshopRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateTkshop(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addTkshop(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除tk商店管理编号为"' + _ids + '"的数据项？').then(function() {
    return delTkshop(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('mxerp/tkshop/export', {
    ...queryParams.value
  }, `tkshop_${new Date().getTime()}.xlsx`)
}

loadSelData();
getList();
</script>
